//
// Created by interset-wq on 2025/10/26.
//

#include "game.h"


void menu(void) {
    printf("*****************************\n");
    printf("********  1. play   *********\n");
    printf("********  0. exit   *********\n");
    printf("*****************************\n");
}

void init_board(char board[N][N])
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            board[i][j] = ' ';
        }
    }
}


void display_board(char board[N][N]) {
    printf("Current board:\n");
    printf(" \t");

    // print col num
    for (int k = 0; k < N; k++) {
        printf(" \033[34m%d  ", k);
    }
    printf("\033[0m\n\n");

    // print board
    for (int i = 0; i < N; i++) {
        printf("\033[34m%d\033[0m\t", i);
        char elem;
        for (int j = 0; j < N; j++) {
            elem = board[i][j];
            if (elem == PLAYER) printf(" \033[32m%c\033[0m", elem);
            else if (elem == PC) printf(" \033[31m%c\033[0m", elem);
            else printf(" %c", elem);
            if (j < N - 1) printf(" |");
        }

        if (i < N - 1) {
            printf("\n \t");
            for (int j = 0; j < N - 1; j++) {
                printf("---|");
            }
            printf("---\n");
        }
    }
    printf("\n\n");
}

bool is_valid(char board[N][N], int x, int y) {
    if (x < 0 || x >= N || y < 0 || y >= N) return false;
    if (board[x][y] != ' ') return false;
    return true;
}

void player_move(char board[N][N]) {
    int x, y;
    printf("It time to move!\nPlease enter your position: ");
    scanf("%d %d", &x, &y);
    while (true) {
        if (is_valid(board, x, y)) {
            board[x][y] = PLAYER;
            break;
        } else {
            printf("Invalid position!\n");
            printf("Please enter your position: ");
            scanf("%d %d", &x, &y);
        }
    }
    printf("You have occupied the position (%d, %d)\n\n", x, y);
    display_board(board);
}

void pc_move(char board[N][N]) {
    int x , y;

    x = -1;
    y = -1;

    srand((unsigned) time(NULL));

    while (true) {
        x = rand() % N;
        y = rand() % N;
        if (is_valid(board, x, y)) {
            board[x][y] = PC;
            break;
        }
    }
    printf("The computer has occupied the position (%d, %d)\n\n", x, y);
    Sleep(1000);
    display_board(board);
}

bool is_win(char board[N][N], char mark) {
    // check row and col
    for (int i = 0; i < N; i++) {
        if (board[i][0] == mark && board[i][1] == mark && board[i][2] == mark) return true;
        else if (board[0][i] == mark && board[1][i] == mark && board[2][i] == mark) return true;
    }
    // check dia
    if (board[0][0] == mark && board[1][1] == mark && board[2][2] == mark) return true;
    else if (board[0][2] == mark && board[1][1] == mark && board[2][0] == mark) return true;
    return false;
}

bool is_full(char board[N][N]) {
    int counter = 0;
    for (int i=0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (board[i][j] != ' ') counter++;
        }
    }
    if (counter == N*N) return true;
    else return false;
}

void game() {
    char board[N][N];

    init_board(board);
    display_board(board);
    while (true) {
        player_move(board);

        if (is_win(board, PLAYER)) {
            printf("Player wins!\n");
            break;
        }
    
        if (is_full(board)) {
            printf("None win!\n");
            break;
        }
        pc_move(board);
  
        if (is_win(board, PC)) {
            printf("Computer wins!\n");
            break;
        }

    }
}